﻿using UnityEngine;
using System.Collections;

public class TotemBehavior : BehaviorUnits 
{
    public int _costHarvester = 1;
    public int _costWarrior = 3;

    enum ListUnit
    {
        Harvester,
        Warrior
    }

    public GameObject _prefabHaverster;
    public GameObject _prefabWarrior;


	
	// Update is called once per frame


    void OnGUI()
    {
        if ((_isDirectSelected || _isSelected) && networkView.isMine)
        {
            if (GUI.Button(new Rect(10, 100, 120, 20), "Build harvester"))
            {
                BuildUnit(ListUnit.Harvester);
            }
            if (GUI.Button(new Rect(10, 140, 120, 20), "Build warrior"))
            {
                BuildUnit(ListUnit.Warrior);
            }
        }
    }

    void BuildUnit(ListUnit unit)
    {
        if (unit == ListUnit.Harvester)
        {
            /*
             *  Create a real check for the unit to spawn
             */
            if (HandleRessource._totalRessource >= _costHarvester)
            {
                HandleRessource._totalRessource -= _costHarvester;
                Vector3 positionToSpawn = new Vector3(this.transform.position.x + 1, this.transform.position.y, this.transform.position.z);
                Network.Instantiate(_prefabHaverster, positionToSpawn, Quaternion.identity, 0);
            }   
        }
        if (unit == ListUnit.Warrior)
        {
            if (HandleRessource._totalRessource >= _costWarrior)
            {
                HandleRessource._totalRessource -= _costWarrior;
                Vector3 positionToSpawn = new Vector3(this.transform.position.x + 1, this.transform.position.y, this.transform.position.z);
                Network.Instantiate(_prefabWarrior, positionToSpawn, Quaternion.identity, 0);
            }
        }
    }

    protected override void HandleSelection()
    {
        _isTotemSelected = (_isSelected || _isDirectSelected);
        base.HandleSelection();
    }

    
}
